home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Shark Attack / Sources & Headers / NewSprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-08  |  7.7 KB  |  255 lines  |  [TEXT/CWIE]

  1. #include <SWIncludes.h>
  2. #include <SWGameUtils.h>
  3. #include <SWApplication.h>
  4.  
  5. #include "Application.h"
  6. #include "Shark Attack.h"
  7. #include "SpriteMoveProcs.h"
  8. #include "NewSprite.h"
  9. #include "SpriteCollideProcs.h"
  10. #include "Stats.h"
  11. #include "GlobalVariables.h"
  12.  
  13.  
  14.     // Pointers to the original sprites which we clone
  15.     // when we want to add a sprite to the animation.
  16.     // We never add these sprites themselves to the animation.
  17.     
  18. SpritePtr    gMasterSubSpriteP,
  19.             gMasterBulletSpriteP,
  20.             gMasterFishSpriteP,
  21.             gMasterSharkSpriteP,
  22.             gMasterTitleSpriteP;
  23.  
  24.  
  25. #define        kFishStrength    2        // Number of shots it takes to kill a fish
  26. #define     kSharkStrength    15        // Number of shots it takes to kill a shark
  27.  
  28.  
  29. ///--------------------------------------------------------------------------------------
  30. // LoadSprites
  31. ///--------------------------------------------------------------------------------------
  32.  
  33. void    LoadSprites( void )
  34. {
  35.     OSErr            err;
  36.     
  37.         // Load gMasterSubSpriteP
  38.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterSubSpriteP, 
  39.             NULL, 128, 2, kFatMask);
  40.     FatalError(err);
  41.     SetUpSprite(gMasterSubSpriteP);
  42.     
  43.         // Load gMasterBulletSpriteP
  44.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBulletSpriteP, 
  45.             NULL, 130, 1, kFatMask);
  46.     FatalError(err);
  47.     SetUpSprite(gMasterBulletSpriteP);
  48.     
  49.         // Load gMasterFishSpriteP
  50.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterFishSpriteP, 
  51.             NULL, 300, 300, 6, kFatMask);
  52.     FatalError(err);
  53.     SetUpSprite(gMasterFishSpriteP);
  54.  
  55.     
  56.         // Load gMasterSharkSpriteP
  57.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterSharkSpriteP, 
  58.             NULL, 200, 200, 6, kFatMask);
  59.     FatalError(err);
  60.     SetUpSprite(gMasterSharkSpriteP);
  61.     
  62.         // Load gMasterTitleSpriteP
  63.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterTitleSpriteP, 
  64.             NULL, 128, 128, 1, kFatMask);
  65.     FatalError(err);
  66.     SetUpSprite(gMasterTitleSpriteP);
  67. }
  68.  
  69.  
  70. ///--------------------------------------------------------------------------------------
  71. // DisposeSprites - we must dispose of each sprite we loaded, since they are not
  72. // added to the SpriteWorld, and therefore will not be disposed with the SpriteWorld.
  73. ///--------------------------------------------------------------------------------------
  74.  
  75. void    DisposeSprites( void )
  76. {
  77.     SWDisposeSprite(&gMasterSubSpriteP);
  78.     SWDisposeSprite(&gMasterBulletSpriteP);
  79.     SWDisposeSprite(&gMasterFishSpriteP);
  80.     SWDisposeSprite(&gMasterSharkSpriteP);
  81.     SWDisposeSprite(&gMasterTitleSpriteP);
  82. }
  83.  
  84.  
  85. ///--------------------------------------------------------------------------------------
  86. // SetUpSprite - makes the LoadSprites code a little cleaner.
  87. ///--------------------------------------------------------------------------------------
  88.  
  89. void    SetUpSprite(SpritePtr mySpriteP)
  90. {
  91.     Rect    moveBoundsRect;
  92.     
  93.     if (gSpriteWorldP->pixelDepth == 8)
  94.         SWCompileSprite(mySpriteP);
  95.     
  96.         // we have to do this since the game rect is smaller than the title screen rect
  97.     moveBoundsRect = gSpriteWorldP->backRect;
  98.     moveBoundsRect.bottom -= kStatsHeight;
  99.     SWSetSpriteMoveBounds(mySpriteP, &moveBoundsRect);
  100.     SWSetSpriteDrawProc(mySpriteP, gSpriteMaskDrawProc);
  101.     SWLockSprite(mySpriteP);
  102. }
  103.  
  104.  
  105. ///--------------------------------------------------------------------------------------
  106. // NewSubSprite - clone the master sprite and add the clone to the animation.
  107. // Also allocate memory for the custom sprite structure.
  108. ///--------------------------------------------------------------------------------------
  109.  
  110. SpritePtr    NewSubSprite( void )
  111. {
  112.     SubStructPtr    subStructP;
  113.     SpritePtr        subSpriteP;
  114.     OSErr            err;
  115.     
  116.         // Allocate memory for the SubStruct
  117.     subStructP = (SubStructPtr)NewPtr(sizeof(SubStruct));
  118.     FatalError( MemError() );
  119.  
  120.     err = SWCloneSprite(gMasterSubSpriteP, &subSpriteP, subStructP);
  121.     FatalError(err);
  122.     
  123.     SWAddSprite(gSubSpriteLayerP, subSpriteP);
  124.     SWSetSpriteMoveProc(subSpriteP, SubSpriteMoveProc);
  125.     SWSetSpriteCollideProc(subSpriteP, SubSpriteCollideProc);
  126.     
  127.     subStructP->horizDelta = 0;
  128.     subStructP->vertDelta = 0;
  129.     subStructP->horizPos = gSpriteWorldP->backRect.right/2-20;
  130.     subStructP->vertPos = gSpriteWorldP->backRect.bottom/2-20;
  131.     
  132.     subStructP->curDirection = kLeftDirection;
  133.     subStructP->numBulletsOnScreen = 0;
  134.     subStructP->nextShotDelay = 0;
  135.     subStructP->canShoot = true;
  136.     
  137.     return subSpriteP;
  138. }
  139.  
  140.  
  141. ///--------------------------------------------------------------------------------------
  142. // NewBulletSprite - clone the master sprite and add the clone to the animation.
  143. // Also allocate memory for the custom sprite structure.
  144. ///--------------------------------------------------------------------------------------
  145.  
  146. SpritePtr    NewBulletSprite( void )
  147. {
  148.     BulletStructPtr    bulletStructP;
  149.     SpritePtr        bulletSpriteP;
  150.     OSErr            err;
  151.     
  152.         // Allocate memory for the BulletStruct
  153.     bulletStructP = (BulletStructPtr)NewPtr(sizeof(BulletStruct));
  154.     FatalError( MemError() );
  155.  
  156.     err = SWCloneSprite(gMasterBulletSpriteP, &bulletSpriteP, bulletStructP);
  157.     FatalError(err);
  158.     
  159.     SWAddSprite(gBulletSpriteLayerP, bulletSpriteP);
  160.     SWSetSpriteMoveProc(bulletSpriteP, BulletSpriteMoveProc);
  161.  
  162.     return bulletSpriteP;
  163. }
  164.  
  165.  
  166. ///--------------------------------------------------------------------------------------
  167. // NewFishSprite - clone the master sprite and add the clone to the animation.
  168. // Also allocate memory for the custom sprite structure.
  169. ///--------------------------------------------------------------------------------------
  170.  
  171. SpritePtr    NewFishSprite( void )
  172. {
  173.     FishStructPtr    fishStructP;
  174.     SpritePtr        fishSpriteP;
  175.     OSErr            err;
  176.     
  177.         // Allocate memory for the FishStruct
  178.     fishStructP = (FishStructPtr)NewPtr(sizeof(FishStruct));
  179.     FatalError( MemError() );
  180.  
  181.     err = SWCloneSprite(gMasterFishSpriteP, &fishSpriteP, fishStructP);
  182.     FatalError(err);
  183.     
  184.     SWAddSprite(gFishSpriteLayerP, fishSpriteP);
  185.     SWSetSpriteMoveProc(fishSpriteP, FishSpriteMoveProc);
  186.     SWSetSpriteCollideProc(fishSpriteP, FishSpriteCollideProc);
  187.     SWSetSpriteFrameTime(fishSpriteP, 1000/15);
  188.     SWSetSpriteFrameAdvanceMode(fishSpriteP, kSWPatrollingMode);
  189.     
  190.     fishStructP->energy = kFishStrength;
  191.     gNumFishOnScreen++;
  192.     fishStructP->moveDelay = 0;
  193.     fishStructP->hitCounter = 0;
  194.  
  195.     return fishSpriteP;
  196. }
  197.  
  198.  
  199. ///--------------------------------------------------------------------------------------
  200. // NewSharkSprite - clone the master sprite and add the clone to the animation.
  201. // Also allocate memory for the custom sprite structure.
  202. ///--------------------------------------------------------------------------------------
  203.  
  204. SpritePtr    NewSharkSprite( void )
  205. {
  206.     SharkStructPtr    sharkStructP;
  207.     SpritePtr        sharkSpriteP;
  208.     OSErr            err;
  209.     
  210.         // Allocate memory for the SharkStruct
  211.     sharkStructP = (SharkStructPtr)NewPtr(sizeof(SharkStruct));
  212.     FatalError( MemError() );
  213.  
  214.     err = SWCloneSprite(gMasterSharkSpriteP, &sharkSpriteP, sharkStructP);
  215.     FatalError(err);
  216.     
  217.     SWAddSprite(gSharkSpriteLayerP, sharkSpriteP);
  218.     SWSetSpriteMoveProc(sharkSpriteP, SharkSpriteMoveProc);
  219.     SWSetSpriteCollideProc(sharkSpriteP, SharkSpriteCollideProc);
  220.     SWSetSpriteFrameTime(sharkSpriteP, 1000/5);
  221.     SWSetSpriteFrameAdvanceMode(sharkSpriteP, kSWPatrollingMode);
  222.     
  223.     sharkStructP->horizDelta = 0;
  224.     sharkStructP->vertDelta = 0;
  225.     
  226.     sharkStructP->energy = kSharkStrength;
  227.     sharkStructP->moveDelay = 0;
  228.     sharkStructP->hitCounter = 0;
  229.     gNumSharksOnScreen++;
  230.  
  231.     return sharkSpriteP;
  232. }
  233.  
  234.  
  235. ///--------------------------------------------------------------------------------------
  236. // AddTitleSprite
  237. ///--------------------------------------------------------------------------------------
  238.  
  239. void    AddTitleSprite( void )
  240. {
  241.     SpritePtr        titleSpriteP;
  242.     Rect            titleSpriteRect;
  243.     OSErr            err;
  244.     
  245.     err = SWCloneSprite(gMasterTitleSpriteP, &titleSpriteP, NULL);
  246.     FatalError(err);
  247.     
  248.     SWAddSprite(gSubSpriteLayerP, titleSpriteP);
  249.     
  250.     titleSpriteRect = titleSpriteP->curFrameP->frameRect;
  251.     CenterRect(&titleSpriteRect, &gSpriteWorldP->backRect);
  252.     SWSetSpriteLocation(titleSpriteP, titleSpriteRect.left, titleSpriteRect.top);
  253. }
  254.  
  255.